package itbour.onetouchshow.widget.inching;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import itbour.onetouchshow.R;
import itbour.onetouchshow.adapter.InchingChangeTabColorAdapter;
import itbour.onetouchshow.adapter.InchingChangeTextColorAdapter;
import itbour.onetouchshow.singlecase.InchingDataSingleCase;

/**
 * Created by zjl_d on 2017/12/18.
 */

public class InchingTableOperaPanel extends LinearLayout {
    private Context context;
    private TextView colorCancel;
    private TextView colorEnsure;
    private OnPanelButtonClick onPanelButtonClick;

    public InchingTableOperaPanel(Context context) {
        this(context, null);
    }

    public InchingTableOperaPanel(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InchingTableOperaPanel(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        //实现该接口的activity 使用该界面的activity
        onPanelButtonClick = (OnPanelButtonClick) context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        //初始化view
        findView();
        //初始化监听
        initClickListener();
    }

    private void initClickListener() {
        colorCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onPanelButtonClick.onTableColorCancel();
            }
        });

        colorEnsure.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onPanelButtonClick.onTableColorEnsure();
            }
        });
    }

    private void findView() {
        colorCancel = findViewById(R.id.tv_tab_color_cancel);
        colorEnsure = findViewById(R.id.tv_tab_corlor_ensure);
        RelativeLayout colorContain = findViewById(R.id.rl_tab_change_color_contain);
        RecyclerView rvColor = new RecyclerView(context);

        List<List<Integer>> colorList = InchingDataSingleCase.INSTANCE.getColorList();

        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 6);
        rvColor.setLayoutManager(gridLayoutManager);
        InchingChangeTabColorAdapter rvColorAdapter = new InchingChangeTabColorAdapter(context, colorList);
        rvColor.setAdapter(rvColorAdapter);
        colorContain.addView(rvColor);
        ((DefaultItemAnimator) rvColor.getItemAnimator()).setSupportsChangeAnimations(false);

        rvColorAdapter.setOnTextColorItemClick(new InchingChangeTextColorAdapter.OnTextColorItemClick() {
            @Override
            public void onTextColorClick(int r, int g, int b, String type) {
                onPanelButtonClick.onTableColorChange(r, g, b, type);

            }
        });

    }


    public interface OnPanelButtonClick {
        void onTableColorChange(int r, int g, int b, String type);

        void onTableColorEnsure();

        void onTableColorCancel();

    }
}
